import { _decorator, Component, director, find, instantiate, math, Node, Prefab, Sprite, SpriteFrame, tween, Tween, v3, Vec3 } from 'cc';
import { Global } from '../Global';
import { FishMgr } from './FishMgr';
import { Tools } from '../Tools';
import { FishItem } from './FishItem';
import { FishEvent } from '../Enums';
import { BulletLine } from './BulletLine';
import { CannonBullet } from './CannonBullet';
import { BulletNet } from './BulletNet';
import { gameMgr } from '../../gameMgr';
const { ccclass, property } = _decorator;

@ccclass('CannonControl')
export class CannonControl extends Component {

    @property(Node)
    cannon: Node = null;
    
    @property(Node)
    pao_s: Node = null;
    @property(Node)
    pao_t: Node = null;

    //填装炮弹
    @property(Prefab)
    bulletPre: Prefab = null;

    bulletsSpr: SpriteFrame[] = [];

    //子弹线
    @property(Prefab)
    bullet_linePre: Prefab = null;
    @property(Prefab)
    bullet_netPre: Prefab = null;

    @property(Number)
    /** 旋转速度 */
    angle_speed: number = 50;
    /** 炮的旋转状态 0 停 1 加 2 减 */
    _angle_stage = 0;

    @property(Node)
    /** 炮口(用于计算向量,炮方向的路径) */
    muzzle: Node = null;

    
    @property(Prefab)
    coin_pre:Prefab = null;

    @property(Node)
    bullet_par: Node = null;
    
    static Inst: CannonControl = null;

    onLoad() {
        CannonControl.Inst = this;

        director.on(FishEvent.addCannon, this.onFillConnon, this);
        
        director.on(FishEvent.openNet, this.onOpenNet, this);
    }

    start() {
        this.bulletsSpr = Global.paoImgs;
        this._angle_stage = 1;
    }

    update(dt: number) {
        if (this._angle_stage == 1) {
            this.node.angle += dt * this.angle_speed;
            if (this.node.angle > 60) {
                this._angle_stage = 2;
            }
        }
        else if (this._angle_stage == 2){
            this.node.angle -= dt * this.angle_speed;
            if (this.node.angle < -60) {
                this._angle_stage = 1;
            }
        }
        else{

        }
    }

    cur_target_node:Node;

    /** 加炮弹，准备发射 */
    onFillConnon(pos: Vec3, indexSpr) {
        let bullet = instantiate(this.bulletPre);
        bullet.setParent(this.node.parent);

        let bpos = this.node.getPosition();
        
        bullet.position = v3(bpos.x, bpos.y - 100, 0);

        bullet.getChildByName('lv').getComponent(Sprite).spriteFrame = this.bulletsSpr[indexSpr];
        bullet.getComponent(CannonBullet).onFireBullet(bpos, this.node);
        
        this.onFrieBullet(indexSpr);
        

        this.scheduleOnce(() => {
            if (this.cur_target_node) {
                let _num = this.cur_target_node.getComponent(FishItem).generate_coin;
                //生成金币数量
                for (let i = 0; i < _num; i++) {
                    this.coin_anim(this.cur_target_node.getWorldPosition(), i * 0.15);
                }
            }
        }, 1.2)
    }

    //金币动画
    coin_anim(pos:Vec3 , delay_time:number){
        let _coin = instantiate(this.coin_pre)
        _coin.setParent(gameMgr.Inst.coin_spr.node)

        _coin.setWorldPosition(pos);
        tween(_coin).delay(delay_time).to(0.35 , {position:Vec3.ZERO}).call(()=>{ _coin.removeFromParent() }).start();
    }

    /** 炮发射动画 */
    onFrieAnim(){
        const _time = 0.07;
        const val1 = 1.1;
        const val2 = 0.9;
        tween( this.pao_s).to(_time , {scale : v3(val2 , 1, 1)})
                            .to(_time , {scale : v3(val1 , val2, 1)})
                            .to(_time , {scale : v3(1 , val2, 1)})
                            
                            .to(_time , {scale : v3(1 , 1, 1)})
                            .start();
        
        tween( this.pao_t).to(_time , {scale : v3(val1 , 1, 1)})
                            .to(_time , {scale : v3(val2 , val1, 1)})
                            .to(_time , {scale : v3(val1 , 1, 1)})
                            .to(_time , {scale : v3(1 , 1, 1)})
                            .start();
    }

    /** 发射子弹 */
    onFrieBullet(indexSpr: number) {
        let fish_near: FishItem = null;

        let _fishs = FishMgr.Inst.findScene_middle();
        if (_fishs.length < 1) { console.log(_fishs.length); return; }

        fish_near = FishMgr.Inst.findLine_near(_fishs);
        if (!fish_near) {
            console.log('场景内没有打击目标')
            return;
        }

        for (let i = 0; i < 3; i++) {
            this.scheduleOnce(() => {
                this.onFrieBullet_hit(i,indexSpr,fish_near)
            }, 0.2 * i + 0.5)//1.1

            //扣血
            fish_near.subLife_val(1)

        }
    }

    onFrieBullet_hit(bullet_num: number, indexSpr: number, fish_near: FishItem) {

        this.onFrieAnim();

        let bullet_line = instantiate(this.bullet_linePre);
        bullet_line.setParent(this.bullet_par);
        bullet_line.setPosition(this.node.getPosition())
        bullet_line.setScale(0, 0, 0);

        const pos = fish_near.node.getPosition();
        let _pos = pos.clone();
        this.cur_target_node = fish_near.node;

        //点相减，得向量 b-a=ab 鱼的点-炮的点
        const x2 = _pos.subtract(this.node.getPosition()).normalize();
        //传入向量，Vec3.UP 是你节点往哪个方向就是哪个方向
        let _angle = Tools.getAngle(Vec3.UP, x2);

        bullet_line.angle = _angle;//子弹旋转角度

        this._angle_stage = 0;
        this.node.angle = _angle;//炮弹旋转角度

        bullet_line.getComponent(BulletLine).setBullet(indexSpr);
        bullet_line.getComponent(BulletLine).onFireMove(pos);

        this.scheduleOnce(() => {
            //director.emit(FishEvent.openNet , pos , fish_near)
            this.onOpenNet(pos, fish_near, indexSpr)
        }, 0.3)


        if (bullet_num < 2) {
            //this.delayFrieBullet(bullet_num , fish_near);
        }
        else {
            this._angle_stage = 1;
        }
    }


    /** 开网 */
    onOpenNet(pos: Vec3 , fish_near:FishItem , indexSpr:number) {
        console.log("开网")

        let bullet_net = instantiate(this.bullet_netPre);
        bullet_net.setParent(this.node.parent);
        bullet_net.setPosition(pos)
        bullet_net.setScale(Vec3.ZERO);


        bullet_net.getComponent(BulletNet).onOpenNet()

        //FishMgr.Inst.fishSubLife_val(fish_near);
        // director.emit(FishEvent.attackFish  , fish_near)
        
        this.scheduleOnce(() => {
                fish_near.judge_anim(indexSpr)
            }, 0.2)
    }

}


